package com.source.hotelbath.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Parcelable;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ffcs.network.connect.CustomerError;
import com.ffcs.network.connect.ResponseListener;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.source.hotelbath.R;
import com.source.hotelbath.base.BizActivity;
import com.source.hotelbath.model.AllGoodsListBean;
import com.source.hotelbath.model.GoodsListBean;
import com.source.hotelbath.net.CustomerRequestConstants;
import com.source.hotelbath.net.request.GoodsLitRequest;
import com.source.hotelbath.net.response.GoodsListResponse;
import com.source.hotelbath.net.response.HotelGoodListResponse;
import com.source.hotelbath.utils.DialogCallback;
import com.source.hotelbath.weight.listview.ptr.PtrMaterialHeader;
import com.source.hotelbath.weight.listview.ptr.ptr.PtrExpandableListView;
import com.source.hotelbath.weight.listview.ptr.ptrloadmore.PtrLoadMoreBase;
import com.source.hotelbath.weight.listview.ptr.ptrloadmore.PtrLoadMoreExpandableListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 添加布草的界面
 */
public class TitleLinenActivity extends BizActivity implements PtrLoadMoreBase.LoadingListener {

    public static final String TAG = TitleLinenActivity.class.getSimpleName();

    private PtrLoadMoreExpandableListView ptr_ebl;
    private TitleExpandAdapter expandAdapter;
    private List<GoodsListBean> commonGoods;
    private List<AllGoodsListBean> allGoodsList=new ArrayList<>();

    private LinearLayout ll_header_left;
    private LinearLayout ll_header_right;
    private View footerView;
    private Button btn_sure;
    private  boolean isRefresh =true;

    @Override
    protected void onBaseCreate(Bundle var1) {
        setContentView(R.layout.activity_title_linen);
    }

    @Override
    protected void bindView(Bundle var1) {

        commonGoods=getIntent().getParcelableArrayListExtra("commonGoods");

        ll_header_left = (LinearLayout) findViewById(R.id.ll_header_left);

        ll_header_right = (LinearLayout) findViewById(R.id.ll_header_right);
        ll_header_right.setVisibility(View.GONE);

        ll_header_left.setOnClickListener(this);
        TextView tv_header_title= (TextView) findViewById(R.id.tv_header_title);
        tv_header_title.setText("商品库");
        initFootView();
        ptr_ebl = (PtrLoadMoreExpandableListView) findViewById(R.id.ptr_ebl);


        ptr_ebl.setLastUpdateKey(TAG);
        ptr_ebl.getRefreshView().setVerticalScrollBarEnabled(false);
        ptr_ebl.registListener(this);
        ptr_ebl.disableWhenHorizontalMove(true);
        ptr_ebl.setHeader(new PtrMaterialHeader(this));
        ptr_ebl.getPtrFrame().setPinContent(true);
        if (footerView!=null){
            ptr_ebl.getRefreshView().addFooterView(footerView);
        }

        expandAdapter = new TitleExpandAdapter(this,allGoodsList);
        ptr_ebl.getRefreshView().setAdapter(expandAdapter);
        ptr_ebl .getRefreshView().setGroupIndicator(null);
        ptr_ebl.setPageSize(15);
        ptr_ebl.loadSuccess();
        ptr_ebl.setHasNext(false);
        ptr_ebl.getRefreshView().setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                ImageView iv_select= (ImageView) v.findViewById(R.id.iv_select);
                if (allGoodsList.get(groupPosition).getGoodsList().get(childPosition).getCheckType()==0){
                    allGoodsList.get(groupPosition).getGoodsList().get(childPosition).setCheckType(1);
                    iv_select.setImageResource(R.drawable.select);
                }else{
                    allGoodsList.get(groupPosition).getGoodsList().get(childPosition).setCheckType(0);
                    iv_select.setImageResource(R.drawable.un_select);
                }
                return true;
            }
        });
    }

    private void initFootView() {
        footerView = LayoutInflater.from(this).inflate(R.layout.item_submit, null);
        btn_sure = (Button) footerView.findViewById(R.id.btn_sure);
        btn_sure.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view==ll_header_left){
            finish();
        }else if (view==btn_sure){
            commonGoods.clear();
            for (int i=0;i<allGoodsList.size();i++){
                for (int j=0;j<allGoodsList.get(i).getGoodsList().size();j++){
                    if (allGoodsList.get(i).getGoodsList().get(j).getCheckType()==1){
                        allGoodsList.get(i).getGoodsList().get(j).setCheckType(0);
                        commonGoods.add(allGoodsList.get(i).getGoodsList().get(j));
                    }
                }
            }
            Log.d("====",commonGoods.size()+"");
            Intent intent = new Intent(CustomerRequestConstants.ALLGOODS);
            intent.putParcelableArrayListExtra("commonGoods", (ArrayList<? extends Parcelable>) commonGoods);
            LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
            finish();
        }
    }

    @Override
    protected void afterCreate() {
        super.afterCreate();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                ptr_ebl.autoRefresh();
            }
        },300);
    }


    @Override
    public void refresh(int pageIndex, int pageSize) {
        if (isRefresh) {
            OkGo.<GoodsListResponse>get(CustomerRequestConstants.newUrl + CustomerRequestConstants.GOODS_COMMOND)//
                    .tag(this)//
                    .execute(new DialogCallback<GoodsListResponse>(TitleLinenActivity.this, false) {
                        @Override
                        public void onSuccess(Response<GoodsListResponse> response) {
                            GoodsListResponse responses = response.body();
                            if (responses != null && responses.getAllGoodsList() != null) {
                                //移除重复已有的布草
                                allGoodsList.clear();
                                allGoodsList.addAll(responses.getAllGoodsList());
                                for (int j = 0; j < commonGoods.size(); j++) {
                                    for (int k = 0; k < allGoodsList.size(); k++) {
                                        if (commonGoods.get(j).getCatName().equals(allGoodsList.get(k).getCatName())) {
                                            for (int n = 0; n < allGoodsList.get(k).getGoodsList().size(); n++) {
                                                if (commonGoods.get(j).getId() == allGoodsList.get(k).getGoodsList().get(n).getId()) {
                                                    allGoodsList.get(k).getGoodsList().remove(n);
                                                }
                                            }
                                        }
                                    }
                                }
                                isRefresh=false;
                                expandAdapter.notifyDataSetChanged();

                            }
                            ptr_ebl.loadSuccess();

                        }

                        @Override
                        public void onError(Response<GoodsListResponse> response) {
                            super.onError(response);
                            ptr_ebl.loadSuccess();
                            Toast.makeText(TitleLinenActivity.this, "请求网络失败", Toast.LENGTH_SHORT).show();
                        }

                    });
        }else {
            ptr_ebl.loadSuccess();
        }
    }

    @Override
    public void loadMore(int pageIndex, int pageSize) {

    }
}
